<?php

namespace addons\shopro\services;

use addons\shopro\model\Category as CategoryModel;
use think\Cache;

/**
 * 分类缓存
 */
class CategoryService
{

    /**
     * 商品分类
     * @param $id
     * @return array|bool|mixed|\PDOStatement|string|\think\Model|null
     * @Author ycr <E-mail:yuncran@126.com>
     */
    public function getCategory($id)
    {
        if (Cache::has(CacheEnum::API_CATEGORY)) {
            $data = Cache::get(CacheEnum::API_CATEGORY);
        } else {
            $data = CategoryModel::getCategoryList($id);
            Cache::set(CacheEnum::API_CATEGORY, $data, CacheEnum::API_TIME);
        }
        return $data;
    }


    /**
     * 猫咪分类数据展示
     * @return array|bool|mixed|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @Author ycr <E-mail:yuncran@126.com>
     */
    public function getCatsCategory()
    {
        if (Cache::has(CacheEnum::API_CATS_CATEGORY)) {
            $data = Cache::get(CacheEnum::API_CATS_CATEGORY);
        } else {
            $data = (new \addons\shopro\model\Breed())->where('status', 1)->select();
            Cache::set(CacheEnum::API_CATS_CATEGORY, $data, CacheEnum::API_TIME);
        }
        return $data;
    }


    /**
     * 清理缓存
     * @param $type
     * @return bool
     * @Author ycr <E-mail:yuncran@126.com>
     */
    public function clearCache($type)
    {
        switch ($type) {
            case CacheEnum::API_CATEGORY: #商品分类
                $data = Cache::rm($type);
                break;
            case CacheEnum::API_CATS_CATEGORY: #猫咪分类
                $data = Cache::clear($type);
                break;
            case CacheEnum::API_TAG_HOME: #商品分类
                $data = Cache::clear($type);
                break;

            default:
                $data = Cache::clear();
                break;
        }
        if (!$data) return false;

        return true;
    }


}